﻿package com.cilico.components;

import com.cilico.utils.Constant;
import com.cilico.utils.Utils;
import com.cilico.view.DetailController;
import com.cilico.view.MainView;
import com.cilico.view.BaseView;
import com.cilico.view.FormLayout;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
//import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ScrollView;

/**
 * 该控件分两部分，上部分为水平布局，主要是该控件的标签栏 下部分是每个标签的详细内容。
 * @author lv.xiangan
 *
 */

public class TabBar extends LinearLayout {
    // 该控件上部的水平布局
    public LinearLayout llH_;
    // 该控件下部的垂直滚动域
    public ScrollView scrollDown_;
    // 该控件下部的垂直布局
    public ContentLayout llV_;
    public TabItem tab_;
    ImageView iv_;
    int x_;
    int xDown_;
    // tab单元个数
    int tabSize_;
    // tabbar文字单元总长
    int totalWidth_;
    // 箭头图片的覆盖长度
    public int lengthBitmap_;
    public boolean focus_ = false;
    public boolean hasBorder_ = false;
    public int textColor_; // normal text color.
    BaseView bv_;
    public ComponentListener listener_;

    public static class ContentLayout extends LinearLayout implements Panel {
        public Table parentTable_;
        public String name_;
        private int height_;
        BaseView bv_;

        public ContentLayout(Context context) {
            super(context);
            bv_ = (BaseView) context;
        }

        public void addView(final View child) {
            try {
                if (child instanceof Component) {
                    final Component c = (Component) child;
                    c.mould();
                    BaseView.setComponentTopMargin(c);
                    bv_.runOnUiThread(new Runnable() {

                        public void run() {
                            // TODO Auto-generated method stub
                            ContentLayout.super.addView(c.getCmpView() == null ? child : c.getCmpView());
                        }
                    });

                } else {
                    bv_.runOnUiThread(new Runnable() {
                        public void run() {
                            ContentLayout.super.addView(child);
                        }
                    });
                }
            } catch (Exception e) {
                Utils.printException(e);
            }
        }

        public void addChild(Component cmp) {
            // TODO Auto-generated method stub

        }

        public Component child(int index) {
            // TODO Auto-generated method stub
            return null;
        }

        public int childrenAmount() {
            // TODO Auto-generated method stub
            return 0;
        }

        public void loadAllChildren() {
            // TODO Auto-generated method stub

        }

        public void cleanText() {
            // TODO Auto-generated method stub

        }

        public String getContentText() {
            // TODO Auto-generated method stub
            return null;
        }

        public View getCmpView() {
            // TODO Auto-generated method stub
            return null;
        }

        public void mould() {
            // TODO Auto-generated method stub

        }

        public void setContentText(String text) {
            // TODO Auto-generated method stub

        }

        public void setCssStyle(CssStyle style) {
            // TODO Auto-generated method stub

        }

        public void setCmpHeidht(int height) {
            // TODO Auto-generated method stub

        }

        public void setCmpWidth(int width) {
            // TODO Auto-generated method stub

        }

        public void shrinkWidth() {
            // TODO Auto-generated method stub

        }

        public void removeChild(Component cmp) {
            // TODO Auto-generated method stub

        }

        public int getCmpHeight() {
            return height_;
        }

        public int getCmpWidth() {
            // TODO Auto-generated method stub
            return 0;
        }

        public void setInTable(boolean inTable) {
            // TODO Auto-generated method stub

        }

        public boolean isInTable() {
            // TODO Auto-generated method stub
            return false;
        }

        public CssStyle getCssStyle() {
            // TODO Auto-generated method stub
            return null;
        }

        public void releaseResource(View view) {
            // TODO Auto-generated method stub

        }

        public void setForm(FormLayout formLayout) {
            // TODO Auto-generated method stub

        }

        public FormLayout getForm() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void setEncrypt(String encrypt) {
            // TODO Auto-generated method stub

        }

    }

    public TabBar(Context context) {
        super(context);
        bv_ = (BaseView) context;
        // TODO Auto-generated constructor stub
        tab_ = new TabItem(context, null, null, null, null, null, null, null);
        /* Create and append tab layout. */
        llH_ = new LinearLayout(context);
        // 设置上部布局方式为水平
        llH_.setOrientation(LinearLayout.VERTICAL);
        // 设置该控件对齐方式为横向居中
        llH_.setGravity(Gravity.CENTER_HORIZONTAL);
        // 设置管理器为横向满屏,竖向自适应
        llH_.setLayoutParams(new LayoutParams(Utils.screenWidth_, LayoutParams.WRAP_CONTENT));
        llH_.setHorizontalScrollBarEnabled(true);
        this.addView(llH_);
        /* Create and append content layout. */
        // create vertical scroll view.s

        llV_ = createContentLayout(context);
        if (bv_ instanceof MainView) {
            addView(llV_);
        } else {
            scrollDown_ = new ScrollView(context);
            scrollDown_.setFillViewport(true);
            // 设置该控件输出范围为填充整个TabBar控件
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
                    ViewGroup.LayoutParams.FILL_PARENT);
            lp.bottomMargin = 10;
            scrollDown_.setLayoutParams(lp);
            addView(scrollDown_);
            // create content root layout.
            scrollDown_.addView(llV_);
        }
        setBgColor(Utils.BLACK);
        setBorderColor(Utils.WHITE);
    }

    public void changeTab() {
        listener_.componentAction(this, (BaseView) this.getContext());
        llV_.invalidate();
    }

    public void setBgColor(int color) {
//        bgColor_ = color;
    }

    public void setBorderColor(int color) {
//        borderColor_ = color;
    }

    public String getContentText() {
        // TODO Auto-generated method stub
        return null;
    }

    public void setContentText(String text) {
        // TODO Auto-generated method stub
    }

    /**
     * @param neoLayout
     */
    public void replaceContentView(final ContentLayout neoLayout) {
        if (neoLayout == null) {
            return;
        }
        try {
            bv_.runOnUiThread(new Runnable() {

                public void run() {
                    // TODO Auto-generated method stub
                    scrollDown_.removeAllViews();
                    neoLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
                    neoLayout.setBackgroundColor(0);
                    scrollDown_.addView(neoLayout);
                    scrollDown_.scrollTo(0, 0);
                    llV_ = neoLayout;
                }

            });

        } catch (Exception e) {
            Utils.printException(e);
        }
    }

    public void setComponentLister(DetailController dtCtl) {
        tab_.setComponentLister(dtCtl);
    }

    void addTab(final BaseView bv, final String[] labels, final Bitmap[] bitmaps, final Bitmap on, final Bitmap off) throws Exception {
        bv.runOnUiThread(new Runnable() {

            public void run() {
                llH_.removeAllViews();
                tab_ = new TabItem(bv, labels, bitmaps, null, on, off, Constant.backgroundTab_, Constant.arrTab_);
                if (null != bitmaps) {
                    tab_.isImgUnit_ = true;
                    tab_.height_ = Math.max((int) tab_.font_.getTextSize(), on.getHeight()) + tab_.TAB_BOTTOM_MARGIN
                            + tab_.TAB_TEXT_ABOVE_MARGIN + tab_.TAB_TEXT_BELOW_MARGIN;
                } else {
                    tab_.isImgUnit_ = false;
                    tab_.height_ = (int) tab_.font_.getTextSize() + tab_.TAB_BOTTOM_MARGIN + tab_.TAB_TEXT_ABOVE_MARGIN
                            + tab_.TAB_TEXT_BELOW_MARGIN;
                }

                tab_.height_ = 30;
                if (Constant.arrTab_ != null)
                	tab_.lengthBitmap_ = Constant.arrTab_.getWidth() >> 1;

                // 设置tab在刚进入界面时为隐身状态
                tab_.setVisibility(View.GONE);
                llH_.addView(tab_);

                // 加入上下箭头图片域
                iv_ = new ImageView(bv);
                llH_.addView(iv_);

                iv_.setOnClickListener(new OnClickListener() {
                    // 如果tab控件不可见，则设置为可见，且更换下面图片域的背景图片，同时statusbar也显示
                    // 如果tab控件可见，则设置为不可见，且更换下面图片域的背景图片，同时statusbar不显示

                    public void onClick(View arg0) {
                        // TODO Auto-generated method stub
                        clickArrows(bv);
                    }

                });
            }

        });
    }

    protected void clickArrows(BaseView bv) {
        // TODO Auto-generated method stub
        // 如果tab控件不可见，则设置为可见，且更换下面图片域的背景图片，同时statusbar也显示
        // 如果tab控件可见，则设置为不可见，且更换下面图片域的背景图片，同时statusbar不显示
        StatusBar statusBar = bv.statusBar_;
        if (tab_.isShown()) {
            tab_.setVisibility(View.GONE);
            statusBar.setVisibility(View.GONE);
            // iv_.setImageResource(R.drawable.show_btn);
        } else {
            tab_.setVisibility(View.VISIBLE);
            tab_.requestFocus();
            statusBar.setVisibility(View.VISIBLE);
            // iv_.setImageResource(R.drawable.hide_btn);
        }
    }

    public void reculculateMaxUnitNum() {
        // TODO Auto-generated method stub
        tab_.reculculateMaxUnitNum();
    }

    /**
     * @return
     */
    public ContentLayout getContentLayout() {
        return llV_;
    }

    public static ContentLayout createContentLayout(Context context) {
        ContentLayout layout = new ContentLayout(context);
        // 设置下部布局方式为垂直
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        // llV_.setPadding(0, 5, 0, 5);
        // 設置llV_最小寬度為屏幕寬
        // llV_.setMinimumWidth(Utils.screenWidth_);
        // llV_.setVerticalScrollBarEnabled(true);
        layout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
        return layout;
    }


    public int getContentChildCount() {
        try {
            return llV_.getChildCount();
        } catch (Exception e) {
            return 0;
        }
    }


    public View getContentChild(int index) {
        try {
            return llV_.getChildAt(index);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * @param index
     */
    public void removeContentChild(final int index) {
    	try {
            bv_.runOnUiThread(new Runnable() {
                public void run() {
                    llV_.removeViewAt(index);
                }
            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Utils.printException(e);
        }
    }

    /**
     * @param v
     */
    public void addContentChild(View v) {
        appendChild(v, -1);
    }

    /**
     * @param v
     * @param index
     */
    public void addContentChild(View v, int index) {
        appendChild(v, index);
    }

    // 添加该控件每个标签下方应显示的详细内容
    private void appendChild(final View view, final int index) {
        if (llV_ == null) {
            return;
        }
        bv_.runOnUiThread(new Runnable() {
            public void run() {
                if (index < 0) {
                    llV_.addView(view);
                } else {
                    llV_.addView(view, index);
                }
            }
        });
    }

    /**
     * 
     */
    public void removeAllContentChild() {
        try {
            bv_.runOnUiThread(new Runnable() {

                public void run() {
                    if (llV_ == bv_.jpcx_) {
                        // TODO Auto-generated method stub
                        scrollDown_.removeAllViews();
                        llV_ = createContentLayout(bv_);
                        scrollDown_.addView(llV_);

                    } else {
                        // TODO Auto-generated method stub
                        Utils.releaseResource(llV_);
                        llV_.removeAllViews();

                    }
                }

            });
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Utils.printException(e);
        }
    }

    public void setContentLayoutGravity() {
        try {
            llV_.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Utils.printException(e);
        }
    }

    public int indexOfContentChild(View v) {
        try {
            return llV_.indexOfChild(v);
        } catch (Exception e) {
            return -1;
        }
    }

}
